package HospitalDepartment

import (
	"ToolLibray/jwtx"
	"context"
	"property_api/utils/result"
	"property_api/utils/xform"
	"property_service/property_service"

	"github.com/zeromicro/go-zero/core/logx"
	"property_api/internal/svc"
	"property_api/internal/types"
)

type EditLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EditLogic {
	return &EditLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// Edit 修改医院科室的逻辑
func (l *EditLogic) Edit(req *types.HospitalDepartmentEdit) (resp *types.Response, err error) {
	// 设置租户ID，从上下文中获取
	tenantID := jwtx.GetUserTenantId64(l.ctx)
	// 设置更新用户，从上下文中获取
	userIdStr := xform.Int64ToString(jwtx.GetUserId(l.ctx))

	// 调用服务层方法，修改医院科室记录
	// 服务层方法 HospitalDepartmentEdit 会处理实际的数据库更新操作
	res, err := l.svcCtx.BasicService.HospitalDepartmentEdit(l.ctx, &property_service.HospitalDepartment{
		ParentID:          req.ParentID,          // 父级ID
		ParentCode:        req.ParentCode,        //父级编码
		ParentLs:          req.ParentLs,          // 父级列表
		DepartmentCode:    req.DepartmentCode,    // 科室编号
		HisDepartmentCode: req.HisDepartmentCode, // HIS科室编码
		Name:              req.Name,              // 科室名称
		DepartmentType:    req.DepartmentType,    // 科室类型
		PinyinCode:        req.PinyinCode,        // 拼音简码
		Director:          req.Director,          // 科室负责人
		MaintenanceAuto:   req.MaintenanceAuto,   // 维修自动处理
		Status:            req.Status,            // 状态
		Remark:            req.Remark,            // 备注
		StockID:           req.StockID,           // 仓库ID
		TenantID:          tenantID,              // 租户ID
		UpdatedAtuser:     userIdStr,             // 更新人
	})

	// 如果服务层返回错误，返回统一的错误响应
	if err != nil {
		return result.Err(err).End()
	}

	// 返回成功的响应
	return result.Suc(res).End()
}
